#-------------------------------------------------------------------------------
# Name:       audio_converter_osx.py
# Purpose:    Convert text to audio files
#
# Authors:    Glenn Ramsey <glenn.ramsey@slidespeech.com>
#
# Copyright:  (c) Slidespeech Ltd 2012
# Licence:    MIT license
#-------------------------------------------------------------------------------

import os
import subprocess
import platform

class AudioConfig:
    release, version_info, machine = platform.mac_ver()
    ttsEngine = "Apple OS X {0}".format(release)
    ttsURL = "http://www.apple.com/macosx/"
    ttsExe = "say"
    oggEncoder = "sox"
    mp3Encoder = "sox"

def encodeOgg(path, inFileName, outFileName):
    return subprocess.call([AudioConfig.ttsExe, inFileName, outFileName])

def encodeMp3(path, inFileName, outFileName):
    return subprocess.call(['sox', inFileName, outFileName])

def textToAudio(text, language, path, basename):
    filename = os.path.join(path, basename + '.aiff')
    
    # uses the system voice, which can be set by the user in the settings app
    ret = subprocess.call(['say', '-o', filename,  text])
    if ret:
        raise RuntimeError("Could not generate audio, {0} failed".format(AudioConfig.ttsExe))

    return filename
